package com.wuxianedu.loadimage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView img = (ImageView) findViewById(R.id.img);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true; // 只解析图片的宽高和额外信息，不解析像素
        BitmapFactory.decodeFile("sdcard/img.bmp", opts);

        int height = opts.outHeight; // 图片的高
        int width = opts.outWidth;// 图片的框

        int scaleH = height / getWindowManager().getDefaultDisplay().getHeight();
        int scaleW = width / getWindowManager().getDefaultDisplay().getWidth();

        // 拿到缩放比例
        if (scaleH >= 1 && scaleW >= 1) {

            if (scaleH >= scaleW) {
                opts.inSampleSize = scaleH;
            } else {
                opts.inSampleSize = scaleW;
            }
        }

        opts.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile("sdcard/img.bmp", opts);

        img.setImageBitmap(bitmap);
    }
}
